package atomic;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.stream.IntStream;

/**
 * @Classname LongAccumulatorDemo
 * @Description TODO
 * @Date 2025/1/7 23:03
 * @Author zhuzhenguo
 */
public class LongAccumulatorDemo {

    public static void main(String[] args) {
        LongAccumulator accumulator = new LongAccumulator((x, y) -> 2 + x * y, 1);
        // 可以使用线程池并行计算
        ExecutorService executor = Executors.newFixedThreadPool(8);
        // 1-9的累计计算
        IntStream.range(1, 10).forEach(i -> executor.submit(() -> accumulator.accumulate(i)));

        executor.shutdown();
        while (!executor.isTerminated()) {

        }
        System.out.println(accumulator.getThenReset());
    }

}
